﻿Imports System.Text

Namespace StatementSources
    Public Class CreateSubjectStatementSource
        Inherits BaseStatementSource

        Public Sub New(code As String, _
                       description As String, _
                       name As String, _
                       units As Decimal, _
                       createdBy As Integer)
            Dim stringBuilder As New StringBuilder()
            stringBuilder.Append("INSERT [dbo].[Subjects] ")
            stringBuilder.Append("([Code],[Description],[Name],[Units],[IsActive],[CreatedBy],[CreatedDttm],[ModifiedBy],[ModifiedDttm]) ")
            stringBuilder.Append("VALUES (@Code,@Description,@Name,@Units,@IsActive,@CreatedBy,GETDATE(),@CreatedBy,GETDATE());")

            Sql = stringBuilder.ToString()

            Parameters.AddWithValue("@Code", code)
            Parameters.AddWithValue("@Description", description)
            Parameters.AddWithValue("@Name", name)
            Parameters.AddWithValue("@Units", units)
            Parameters.AddWithValue("@IsActive", True)
            Parameters.AddWithValue("@CreatedBy", createdBy)
        End Sub

    End Class

    Public Class ReadAllSubjectStatementSource
        Inherits BaseStatementSource

        Public Sub New()
            Dim stringBuilder As New StringBuilder()
            stringBuilder.Append("SELECT [SubjectId],[Code],[Description],[Name],[Units],[IsActive],[CreatedBy],[CreatedDttm],[ModifiedBy],[ModifiedDttm] ")
            stringBuilder.Append("FROM [dbo].[Subjects];")

            Sql = stringBuilder.ToString()
        End Sub

    End Class

    Public Class ReadBySubjectIdStatementSource
        Inherits BaseStatementSource

        Public Sub New(subjectId As Integer)
            Dim stringBuilder As New StringBuilder()
            stringBuilder.Append("SELECT [SubjectId],[Code],[Description],[Name],[Units],[IsActive],[CreatedBy],[CreatedDttm],[ModifiedBy],[ModifiedDttm] ")
            stringBuilder.Append("FROM [dbo].[Subjects] ")
            stringBuilder.Append("WHERE [SubjectId]=@SubjectId ")
            stringBuilder.Append("AND [IsActive]=@IsActive;")

            Sql = stringBuilder.ToString()

            Parameters.AddWithValue("@SubjectId", subjectId)
            Parameters.AddWithValue("@IsActive", True)
        End Sub

    End Class

    Public Class UpdateSubjectStatementSource
        Inherits BaseStatementSource

        Public Sub New(subjectId As Integer, _
                       code As String, _
                       description As String, _
                       name As String, _
                       units As Decimal, _
                       modifiedBy As Integer)
            Dim stringBuilder As New StringBuilder()
            stringBuilder.Append("UPDATE [dbo].[Subjects] SET ")
            stringBuilder.Append("[Code]=ISNULL(@Code,[Code]),")
            stringBuilder.Append("[Description]=ISNULL(@Description,[Description]),")
            stringBuilder.Append("[Name]=ISNULL(@Name,[Name]),")
            stringBuilder.Append("[Units]=ISNULL(@Units,[Units]),")
            stringBuilder.Append("[ModifiedBy]=@ModifiedBy,")
            stringBuilder.Append("[ModifiedDttm]=GETDATE() ")
            stringBuilder.Append("WHERE [SubjectId]=@SubjectId;")

            Sql = stringBuilder.ToString()

            Parameters.AddWithValue("@SubjectId", subjectId)
            Parameters.AddWithValue("@Code", code)
            Parameters.AddWithValue("@Description", description)
            Parameters.AddWithValue("@Name", name)
            Parameters.AddWithValue("@Units", units)
            Parameters.AddWithValue("@ModifiedBy", modifiedBy)
        End Sub

    End Class

    Public Class DeleteSubjectStatementSource
        Inherits BaseStatementSource

        Public Sub New(subjectId As Integer)
            Sql = "UPDATE [dbo].[Subjects] SET [IsActive]=ISNULL(@IsActive,[IsActive]) WHERE [SubjectId]=@SubjectId;"
            Parameters.AddWithValue("@SubjectId", subjectId)
            Parameters.AddWithValue("@IsActive", False)
        End Sub

    End Class
End Namespace

